﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Jn698._45GWTool.Models.DataGrid;
using Jn698._45GWTool.Services;
using Jn698._45GWTool.Views;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WebFirst.Entities;

namespace Jn698._45GWTool.ViewModels
{
    public class AddOrUpdateJobViewModel:ViewModelBase
    {
        public static GWCmdMakeUp GWCmdMakeUp;
        private GWCmdMakeUpService gWCmdMakeUpService = new GWCmdMakeUpService();
        private int totalNum;

		public int TotalNum
		{
			get { return totalNum; }
			set { totalNum = value; RaisePropertyChanged(); SaveDataInfo(); GetDataInfo(value); }
		}

        private List<int> totalNumList;

        public List<int> TotalNumList
        {
            get { return totalNumList; }
            set { totalNumList = value; RaisePropertyChanged(); }
        }

        private int jobID;

		public int JobID
		{
			get { return jobID; }
			set { jobID = value; RaisePropertyChanged(); }
		}

		private int fre;

		public int Fre
		{
			get { return fre; }
			set { fre = value; RaisePropertyChanged(); }
		}

		private int freUnitSelected;

		public int FreUnitSelected
		{
			get { return freUnitSelected; }
			set { freUnitSelected = value; RaisePropertyChanged(); }
		}

		private int typeSelected;

		public int TypeSelected
		{
			get { return typeSelected; }
			set { typeSelected = value; RaisePropertyChanged(); }
		}

		private int number;

		public int Number
		{
			get { return number; }
			set { number = value; RaisePropertyChanged(); }
		}

		private DateTime startDateTime;

		public DateTime StartDateTime
		{
			get { return startDateTime; }
			set { startDateTime = value; RaisePropertyChanged(); }
		}

		private DateTime endDateTime;

		public DateTime EndDateTime
		{
			get { return endDateTime; }
			set { endDateTime = value; RaisePropertyChanged(); }
		}

		private int delay;

		public int Delay
		{
			get { return delay; }
			set { delay = value; RaisePropertyChanged(); }
		}
		private int delayUnitSelected;

		public int DelayUnitSelected
		{
			get { return delayUnitSelected; }
			set { delayUnitSelected = value; RaisePropertyChanged(); }
		}

		private int stateSelected;

		public int StateSelected
		{
			get { return stateSelected; }
			set { stateSelected = value; RaisePropertyChanged(); }
		}
		private int level;

		public int Level
		{
			get { return level; }
			set { level = value; RaisePropertyChanged(); }
		}

		private int runTypeSelected;

		public int RunTypeSelected
		{
			get { return runTypeSelected; }
			set { runTypeSelected = value; RaisePropertyChanged(); }
		}

		private int runStartID;

		public int RunStartID
		{
			get { return runStartID; }
			set { runStartID = value; RaisePropertyChanged(); }
		}

		private int runEndID;

		public int RunEndID
		{
			get { return runEndID; }
			set { runEndID = value; RaisePropertyChanged(); }
		}

		private ObservableCollection<string> typeList;

		public ObservableCollection<string> TypeList
		{
			get { return typeList; }
			set { typeList = value; RaisePropertyChanged(); }
		}

		private ObservableCollection<string> freList;

		public ObservableCollection<string> FreList
		{
			get { return freList; }
			set { freList = value; RaisePropertyChanged(); }
		}

		private ObservableCollection<string> delayList;

		public ObservableCollection<string> DelayList
		{
			get { return delayList; }
			set { delayList = value; RaisePropertyChanged(); }
		}

		private ObservableCollection<string> stateList;

		public ObservableCollection<string> StateList
		{
			get { return stateList; }
			set { stateList = value; }
		}

		private ObservableCollection<string> runTypeList;

		public ObservableCollection<string> RunTypeList
		{
			get { return runTypeList; }
			set { runTypeList = value; RaisePropertyChanged(); }
		}

		private ObservableCollection<GWCmdMakeUp> gWCmdMakeUpList;

		public ObservableCollection<GWCmdMakeUp> GWCmdMakeUpList
        {
			get { return gWCmdMakeUpList; }
			set { gWCmdMakeUpList = value; RaisePropertyChanged(); }
		}

        private string groupName;

        public string GroupName
        {
            get { return groupName; }
            set { groupName = value; RaisePropertyChanged(); }
        }

        private int selectedIndex;

		public int SelectedIndex
        {
			get { return selectedIndex; }
			set { selectedIndex = value; RaisePropertyChanged(); GroupName = $"第{value + 1}个配置单元"; UpDataInfo(); }
		}

		private ObservableCollection<JobConfigTimeData> timeList;

		public ObservableCollection<JobConfigTimeData> TimeList
		{
			get { return timeList; }
			set { timeList = value; RaisePropertyChanged(); }
		}

        public AddOrUpdateJobViewModel()
        {
            TotalNumList = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
            TypeList = new ObservableCollection<string>() { "普通采集方案","事件采集方案","透明方案","上报方案","脚本方案","实时监控方案"};
			FreList = new ObservableCollection<string>() { "秒","分","时","日","月","年"};
            DelayList = new ObservableCollection<string>() { "秒", "分", "时", "日", "月", "年" };
			StateList = new ObservableCollection<string>() { "正常","停用"};
			RunTypeList = new ObservableCollection<string>() { "前闭后开","前开后闭","前闭后闭","前开后开"};
			TimeList = new ObservableCollection<JobConfigTimeData>();
        }
        public void GetDataInfo(int total)
        {
            GWCmdMakeUpList = new ObservableCollection<GWCmdMakeUp>(gWCmdMakeUpService.SelectByOAD("60127F00", total));
            if (GWCmdMakeUpList.Count <= 0)
            {
                return;
            }
            SelectedIndex = 0;
            UpDataInfo();
        }
        public void UpDataInfo()
        {
            if (SelectedIndex < 0)
            {
                SelectedIndex = 0;
            }
            if (GWCmdMakeUpList.Count <= 0)
            {
                return;
            }
            //判断之前是否点击过，如果点击过则保存之前的数据
            if (GWCmdMakeUp != null)
            {
                SaveDataInfo();
            }

            GWCmdMakeUp = GWCmdMakeUpList[SelectedIndex];
            if (GWCmdMakeUp == null)
            {
                return;
            }
            try
            {
                TimeList.Clear();
                var datas = GWCmdMakeUp.DataValue.Split(';');
				if (datas.Length<14)
				{
					return;
				}
				JobID = int.Parse(datas[0]);
				FreUnitSelected= int.Parse(datas[1]);
				Fre = int.Parse(datas[2]);
				TypeSelected = int.Parse(datas[3]);
				Number = int.Parse(datas[4]);
				StartDateTime = DateTime.Parse(datas[5]);
				EndDateTime = DateTime.Parse(datas[6]);
				DelayUnitSelected = int.Parse(datas[7]);
				Delay = int.Parse(datas[8]);
				Level = int.Parse(datas[9]);
				StateSelected = int.Parse(datas[10]);
				RunStartID = int.Parse(datas[11]);
				RunEndID = int.Parse(datas[12]);
				RunTypeSelected = int.Parse(datas[13]);
				var adds = datas[14]?.Split(',');
				int num = adds.Length / 4;
				if (num <= 0)
				{
					return;
				}
				for (int i = 0; i < num; i++)
				{
					JobConfigTimeData jobConfigTimeData = new JobConfigTimeData();
                    jobConfigTimeData.StartHour = int.Parse(adds[4 * i]);
                    jobConfigTimeData.StartMin = int.Parse(adds[4 * i+1]);
                    jobConfigTimeData.EndHour = int.Parse(adds[4 * i + 2]);
                    jobConfigTimeData.EndMin = int.Parse(adds[4 * i + 3]);
					TimeList.Add(jobConfigTimeData);
                }
			}
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }


        }

        public void SaveDataInfo()
        {
            if (GWCmdMakeUp == null)
            {
                return;
            }
            StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append(JobID.ToString());
			stringBuilder.Append(";");
            stringBuilder.Append(FreUnitSelected.ToString());
            stringBuilder.Append(";");
            stringBuilder.Append(Fre.ToString());
            stringBuilder.Append(";");
            stringBuilder.Append(TypeSelected.ToString());
            stringBuilder.Append(";");
            stringBuilder.Append(Number.ToString());
            stringBuilder.Append(";");
            stringBuilder.Append(StartDateTime.ToString());
            stringBuilder.Append(";");
            stringBuilder.Append(EndDateTime.ToString());
            stringBuilder.Append(";");
            stringBuilder.Append(DelayUnitSelected.ToString());
            stringBuilder.Append(";");
            stringBuilder.Append(Delay.ToString());
            stringBuilder.Append(";");
            stringBuilder.Append(Level.ToString());
            stringBuilder.Append(";");
            stringBuilder.Append(StateSelected.ToString());
            stringBuilder.Append(";");
            stringBuilder.Append(RunStartID.ToString());
            stringBuilder.Append(";");
            stringBuilder.Append(RunEndID.ToString());
            stringBuilder.Append(";");
            stringBuilder.Append(RunTypeSelected.ToString());
            stringBuilder.Append(";");

			for (int i = 0; i < TimeList.Count; i++)
			{
				JobConfigTimeData jobConfigTimeData = TimeList[i];

                stringBuilder.Append(jobConfigTimeData.StartHour.ToString());
				stringBuilder.Append(",");
				stringBuilder.Append(jobConfigTimeData.StartMin.ToString());
				stringBuilder.Append(",");
                stringBuilder.Append(jobConfigTimeData.EndHour.ToString());
                stringBuilder.Append(",");
                stringBuilder.Append(jobConfigTimeData.EndMin.ToString());
                stringBuilder.Append(",");
            }
			GWCmdMakeUp.DataValue = stringBuilder.ToString();
            gWCmdMakeUpService.Update(GWCmdMakeUp);
        }

		
    }
}
